package com.youxi.game;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.VideoView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    private VideoView backgroundVideo;
    private ImageView mainLogo;
    private ImageView icon;
    private ImageView serverIcon;
    private ImageView textPrompt;
    private ImageView ageRating;
    private ImageView qrCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置全屏和横屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_main);

        initViews();
        setupBackgroundVideo();
        setupUI();
    }

    private void initViews() {
        backgroundVideo = findViewById(R.id.background_video);
        mainLogo = findViewById(R.id.main_logo);
        icon = findViewById(R.id.icon);
        serverIcon = findViewById(R.id.server_icon);
        textPrompt = findViewById(R.id.text_prompt);
        ageRating = findViewById(R.id.age_rating);
        qrCode = findViewById(R.id.qr_code);
    }

    private void setupBackgroundVideo() {
        // 设置背景视频
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.视频);
        backgroundVideo.setVideoURI(videoUri);

        backgroundVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
                backgroundVideo.start();
            }
        });

        backgroundVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                backgroundVideo.start();
            }
        });
    }

    private void setupUI() {
        // 设置UI元素的点击事件等
        mainLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 主logo点击事件
            }
        });

        serverIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 服务器图标点击事件
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (backgroundVideo != null) {
            backgroundVideo.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (backgroundVideo != null) {
            backgroundVideo.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (backgroundVideo != null) {
            backgroundVideo.stopPlayback();
        }
    }
}
